// 引入axios
import axios from 'axios';

import store from '@/store';

// 公共接口
axios.defaults.baseURL = '/api/private/v1/';
// 请求超时
axios.defaults.timeout = 5000;


// 请求拦截器
axios.interceptors.request.use( config => {
  if ( store.state.token ) {
    config.headers.Authorization = store.state.token;
  }
  return config;
}, error => {
  return Promise.reject( error );
} )

// 响应拦截器
axios.interceptors.response.use( response => {
  return response.data;
}, error => {
  return Promise.reject( error );
} )

// 导出接口
// get
export const get = ( url, data = {} ) => {
  return new Promise( ( resolve, reject ) => {
    axios.get( url, { params: data } ).then( res => {
      resolve( res );
    }, err => {
      reject( err );
    } )
  } )
}

// post
export const post = ( url, data ) => {
  return new Promise( ( resolve, reject ) => {
    axios.post( url, data ).then( res => {
      resolve( res );
    }, err => {
      reject( err );
    } )
  } )
}